<template>
  <div>
    <el-card class="box-card">
      <div class="mianbao" v-if="$route.query.id">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item v-if="$route.query.name">{{
            $route.query.name
          }}</el-breadcrumb-item>
          <el-breadcrumb-item>活动详情</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 头部 -->
      <div class="box">
        <div class="box_left">
          <el-form :inline="true" :model="formInline">
            <el-form-item label="标签名称">
              <el-input v-model="formInline.tagName" placeholder=""></el-input>
            </el-form-item>
            <el-form-item label="状态" class="zhuangtai">
              <el-select v-model="formInline.state" placeholder="请选择">
                <el-option label="启用" :value="1"></el-option>
                <el-option label="禁用" :value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="delkeyword">清除</el-button>
              <el-button
                size="small"
                class="colInput"
                type="primary"
                @click="serach()"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="box_right">
          <el-button
            type="text"
            icon="el-icon-back"
            @click="getSubjects"
            v-if="$route.query.id"
            >返回学科</el-button
          >
          <el-button type="success" icon="el-icon-edit" @click="addtages"
            >新增标签</el-button
          >
        </div>
      </div>

      <!-- 消息提示的文案 -->
      <el-alert :title="`数据一共 ${counts} 条`" type="info" show-icon>
      </el-alert>

      <!--表格区域组件 -->
      <el-table
        :data="tagslist"
        style="width: 100%"
        v-loading="isloading"
        class="biaoge"
      >
        <el-table-column
          type="index"
          label="序号"
          :index="indexMethod"
          width="150"
        >
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科" style="width: 10%">
        </el-table-column>
        <el-table-column prop="tagName" label="标签名称" style="width: 10%">
        </el-table-column>
        <el-table-column prop="username" label="创建者" style="width: 10%">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" style="width: 20%">
          <template #default="{ row }">
            {{ formaTime(row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="state"
          label="状态"
          style="width: 10%"
          :formatter="formateDate"
        >
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template #default="{ row }">
            <el-button type="text" size="small" @click="changeState(row)">{{
              row.state === 1 ? '禁用' : '启用'
            }}</el-button>
            <el-button
              type="text"
              size="small"
              @click="edittags(row)"
              :disabled="row.state == 0 ? false : true"
              >修改</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="Deltaga(row.id)"
              :disabled="row.state == 0 ? false : true"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 :current-page:当前页面-->
      <div class="fenyebox">
        <el-pagination
          class="fenye"
          :hide-on-single-page="abc"
          background
          :current-page="queryList.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="queryList.pagesize"
          layout="total,sizes,prev, pager, next, jumper"
          :total="counts"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>

      <!-- 新增标签 组件-->
    </el-card>
    <tagAdd
      v-if="showDialog"
      :show-dialog.sync="showDialog"
      :data="editItem"
    ></tagAdd>
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/tags.js'
import dayjs from 'dayjs'
import tagAdd from '../components/tags-add.vue'

export default {
  // name: 'TagsAdd',
  components: {
    tagAdd
  },
  data () {
    return {
      // 修改数据
      editItem: null,
      formInline: {
        tagName: '',
        state: ''
      },
      counts: 0,
      tagslist: [],
      // 标签请求数据参数
      queryList: {
        page: 1,
        pagesize: 10
      },
      // loading
      isloading: false,
      // 弹框
      showDialog: false,
      // 标签的ID
      infoId: '',
      subjectName: '',
      abc: false
    }
  },

  // 调用
  created () {
    // 这个三元意思 判断 如果不是从学科页面跳转过来的 就使用pagesgoto() 则调用原始方法
    this.$route.query ? this.pagesgoto() : this.lister()
  },
  methods: {
    serach () {
      this.$route.query ? this.pagesgoto() : this.lister()
    },
    // 页面跳转
    async pagesgoto () {
      this.isloading = true
      // this.abc = false
      const { tagName, state } = this.formInline
      const param = { ...this.queryList }
      if (tagName) {
        param.tagName = tagName
      }
      if (state !== '') {
        param.state = state
      }
      const { data } = await list({
        ...this.queryList,
        subjectID: this.$route.query.id,
        tagName: param.tagName,
        state: param.state
      })
      this.tagslist = data.items
      this.counts = data.counts
      this.abc = this.counts <= param.pagesize
      // this.abc = true
      this.isloading = false
    },
    // 修改功能
    async edittags (data) {
      this.editItem = data
      this.showDialog = true
      // await update(id)
      // console.log(data)
    },
    // 点击显示添加弹框
    addtages () {
      this.editItem = null
      this.showDialog = true
    },
    // 删除
    Deltaga (id) {
      // console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
        .finally(() => {
          this.$route.query ? this.pagesgoto() : this.lister()
        })
    },
    // 点击显示禁用启用
    async changeState (data) {
      const { id, state } = data
      await changeState({ id, state: state === 1 ? 0 : 1 })
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      ;(await this.$route.query) ? this.pagesgoto() : this.lister()
    },
    // 获取列表数据
    async lister () {
      this.isloading = true
      // 搜索---获取数据
      const { tagName, state } = this.formInline
      const param = { ...this.queryList }
      if (tagName) {
        param.tagName = tagName
      }
      if (state !== '') {
        param.state = state
      }
      const { data } = await list(param)
      this.tagslist = data.items
      this.counts = data.counts
      this.isloading = false
    },

    // 页容量改变,一页多少条
    handleSizeChange (newSize) {
      this.queryList.pagesize = newSize
      this.$route.query ? this.pagesgoto() : this.lister()
    },
    // 页码改变, 多少页
    handleCurrentChange (newPage) {
      this.queryList.page = newPage
      this.$route.query ? this.pagesgoto() : this.lister()
    },
    // 清空表单
    delkeyword () {
      this.formInline.tagName = ''
      this.formInline.state = ''
      this.$route.query ? this.pagesgoto() : this.lister()
    },
    // 页面跳转
    getSubjects () {
      this.$router.push('/subjects/list')
    },
    // 序号
    indexMethod (index) {
      return (this.queryList.page - 1) * this.queryList.pagesize + index + 1
    },
    // 日期修改
    formaTime (time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 状态的改变
    formateDate (row, column, cellValue, inex) {
      // 启用为1  禁用为0
      if (cellValue === 1) {
        return '已启用'
      }
      if (cellValue === 0) {
        return '已禁用'
      }
    }
  }
}
</script>

<style scoped lang='scss'>
.box-card {
  margin: 10px;
}
.box {
  padding-top: 18px;
  display: flex;
  justify-content: space-between;
}
.zhuangtai {
  padding-left: 50px;
}
.fenyebox {
  float: right;
}
.fenye {
  padding: 20px;
}
.mianbao {
  padding: 0 0 18px 0;
  border-bottom: 1px solid #ebeef5;
  // -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
</style>
